package com.top.mall.main.mine

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.image.loadRoundImage
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.jetpack.vm.CouponViewModel
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.engine.impl.GlideEngine
import kotlinx.android.synthetic.main.activity_mine_feedback.*
import kotlinx.android.synthetic.main.activity_mine_feedback.imageViewBack


class MineFeedbackActivity : OwnerViewModeActivity<CouponViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MineFeedbackActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val photoAdapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_feedback_photo) {

            init {
                data.add(0, "")
                addChildClickViewIds(R.id.ivPhotoDel)
            }

            override fun convert(holder: BaseViewHolder, item: String) {
                holder.getView<ImageView>(R.id.ivPhotoDel).visibility =
                    if (holder.layoutPosition == 0) View.INVISIBLE else View.VISIBLE
                holder.getView<ImageView>(R.id.ivPhotoImage)
                    .loadRoundImage(src = if (holder.layoutPosition == 0) R.mipmap.ic_app_set_add else item)
            }

            override fun setNewInstance(list: MutableList<String>?) {
                list?.add(0, "")
                super.setNewInstance(list)
            }
        }

    override fun viewModel(): CouponViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_feedback

    override fun stateBar(): StatusBar {
        return StatusBar(view = flMineFeedbackBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }

        rvFeedbackPhoto.adapter = photoAdapter

        photoAdapter.setOnItemClickListener { _, _, position ->
            if (position != 0) {
                return@setOnItemClickListener
            }
            Matisse.from(this@MineFeedbackActivity)
                .choose(MimeType.ofImage()) //图片类型
                .countable(true) //true:选中后显示数字;false:选中后显示对号
                .maxSelectable(3) //可选的最大数
                .imageEngine(GlideEngine()) //图片加载引擎
                .theme(R.style.Matisse_Dracula)
                .forResult(100) //
        }
        photoAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.ivPhotoDel) {
                adapter.removeAt(position)
            }
        }
    }

    override fun initData() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == RESULT_OK) {
            photoAdapter.setNewInstance(Matisse.obtainPathResult(data).toMutableList())
        }
    }

}